package com.jd.cmdb.tsc.zk.listener;

import com.jd.cmdb.tsc.zk.service.ZKService;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;

/**
 * Created by tianzhengguo on 2016/1/20.
 */
public class TSCNodeListener implements NodeCacheListener {
    private static final Logger logger = LoggerFactory.getLogger(TSCNodeListener.class);

    private ZKService zkService;
    private CountDownLatch tscNodeLatch;
    private String tscNodePath;

    public TSCNodeListener(ZKService zkService,CountDownLatch tscNodeLatch,String tscNodePath){
        this.zkService = zkService;
        this.tscNodeLatch = tscNodeLatch;
        this.tscNodePath = tscNodePath;
    }
    /**
     * Called when a change has occurred
     */
    @Override
    public void nodeChanged() throws Exception {
        if(zkService.isLeader())
            return ;
        logger.info("{}节点数据发生改变.",this.tscNodePath);
        tscNodeLatch.countDown();
    }
}
